package com.javacoo.swing.jxbrowser.content;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

import com.javacoo.swing.jxbrowser.BaseBrowser;
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.CertificateVerifier;
import com.teamdev.jxbrowser.chromium.CertificateVerifyParams;
import com.teamdev.jxbrowser.chromium.CertificateVerifyResult;
import com.teamdev.jxbrowser.chromium.NetworkService;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

/**
 * SSL证书验证程序
 * <li></li>
 * 在JxBrowser 6.3中，引入了CertificateVerifier API。使用此API，您可以获取有关用于显示HTTPS网页的每个SSL证书的信息，并决定是应该接受还是拒绝它。默认情况下，Chromium引擎决定是否接受/拒绝证书。您可以注册自己的 CertificateVerifier实现来修改默认行为
 * @author: duanyong@jccfc.com
 * @since: 2020/9/25 10:19
 */
public class SSLCertificateVerifier extends BaseBrowser {
    public static void main(String[] args) {
        Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);

        NetworkService networkService = browser.getContext().getNetworkService();
        networkService.setCertificateVerifier(new CertificateVerifier() {
            @Override
            public CertificateVerifyResult verify(CertificateVerifyParams params) {
                // Reject SSL certificate for all "google.com" hosts.
                if (params.getHostName().contains("gitee.com")) {
                    return CertificateVerifyResult.INVALID;
                }
                return CertificateVerifyResult.OK;
            }
        });

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.loadURL(BASE_URL);
    }
}
